package com.sbox.album.model;

import java.io.File;
import android.graphics.Bitmap;
import com.sbox.util.AlbumConst;
import com.sbox.util.AlbumLog;
import com.sbox.util.PictureUtils;

public class ThumbManager {
	
	private static ThumbManager sAlbumCache = null;
	public static ThumbManager getInstance() {
		if( sAlbumCache == null ) {
			sAlbumCache = new ThumbManager();
		}
		return sAlbumCache;
	}
	
	private ThumbManager() {
		File systemRoot = new File(AlbumConst.SYSTEM_IMAGE_CACHE_PATH);
		if( !systemRoot.exists() )
			systemRoot.mkdirs();
		
		File albumRoot = new File(AlbumConst.ALBUM_ROOT_CACHE_PATH);
		if( !albumRoot.exists() )
			albumRoot.mkdirs();
	}
	
	public Bitmap getThumbBitmap(String fileFullName) {
		String thumbName;
		
		//AlbumLog.info("getThumbBitmap: " + fileFullName);
		thumbName = createPictureThumb(fileFullName);
		if( thumbName == null ) {
			AlbumLog.error("createPictureThumb failed: " + fileFullName);
			return null;
		}		
		//AlbumLog.info("createPicture: " + thumbName);
		
		return PictureUtils.createPicture(thumbName);
	}
	
	public String createPictureThumb(String fileName) {
		String fileCacheName = null;
		File file = null;
		File fileCache = null;
		Bitmap bmp = null;
		
		fileCacheName = getCacheFileName(fileName);
		
		file = new File(fileName);		
		if( !file.exists() ) {
			AlbumLog.error("file not exist: " + fileName);
			return null;
		}
		
		fileCache = new File(fileCacheName);
		if( fileCache.exists() )
			return fileCacheName;
		else
		{
			fileCache.getParentFile().mkdirs();
		}
		
		bmp = PictureUtils.createSmallPicture(fileName);
		if( bmp == null ) {
			return null;
		}			
		PictureUtils.savePicture(bmp, fileCacheName);
		
		return fileCacheName;
	}
	
	public void createAlbumThumb(String album) {
		PictureInfo[] pics = AlbumManager.listPictures(album);
		if( pics == null )
			return;
		
		for( PictureInfo pic: pics ) {
			if( pic == null )
				return;
			
			createPictureThumb(pic.getFileName());
		}
	}
	
	public void clearAlbumThumb(String album) {
		String cacheAlbum = null;
		PictureInfo[] cachePics = null;
		
		if( album.startsWith(AlbumConst.SYSTEM_IMAGE_PATH) ) {
			cacheAlbum = AlbumConst.SYSTEM_IMAGE_CACHE_PATH;
		}
		else {
			cacheAlbum = album.replace(AlbumConst.ALBUM_ROOT_PATH, AlbumConst.ALBUM_ROOT_CACHE_PATH);
		}
		
		cachePics = AlbumManager.listPictures(cacheAlbum);
		for( PictureInfo pic: cachePics ) {
			String cacheFileName = null;
			String fileName = null;
			File f = null;
			
			cacheFileName = pic.getFileName();
			fileName = getFileNameFromCache(cacheFileName);
			
			if( !f.exists() ) {
				File cacheF = new File(cacheFileName);
				cacheF.delete();
			}
		}
	}
	
	private String getCacheFileName(String fileName) {
		String cacheFileName = null;
		
		if( fileName.startsWith(AlbumConst.SYSTEM_IMAGE_PATH) ) {
			cacheFileName = fileName.replace(AlbumConst.SYSTEM_IMAGE_PATH, AlbumConst.SYSTEM_IMAGE_CACHE_PATH);
		}
		else {
			cacheFileName = fileName.replace(AlbumConst.ALBUM_ROOT_PATH, AlbumConst.ALBUM_ROOT_CACHE_PATH);
		}
		
		return cacheFileName;
	}
	
	private String getFileNameFromCache(String cacheFileName) {
		String fileName = null;
		
		if( cacheFileName.startsWith(AlbumConst.SYSTEM_IMAGE_CACHE_PATH) ) {
			fileName = cacheFileName.replace(AlbumConst.SYSTEM_IMAGE_CACHE_PATH, AlbumConst.SYSTEM_IMAGE_PATH);
		}
		else {
			fileName = cacheFileName.replace(AlbumConst.ALBUM_ROOT_CACHE_PATH, AlbumConst.ALBUM_ROOT_PATH);
		}
		
		return fileName;
	}
}
